package com.ak.ui;

import com.ak.cluster.AssetIndex;
import com.ak.cluster.GameScreen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.Table;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.TableLayout;

public class ClusterUI {
	private Table mWindow;
	public Skin uiFactory;
	public BitmapFont debugFont;
	
	public String debugString;
	public GameScreen mGameScreen;
	public Stage mStage;
	
	public ClusterUI(Table window,Stage stage, GameScreen lGameScreen)
	{
		mGameScreen = lGameScreen;
		mStage = stage;
		mWindow = window;	
		debugString = "null";
		init();
	}
	private void init()
	{
		uiFactory = new Skin(Gdx.files.internal(AssetIndex.uiSkin), Gdx.files.internal(AssetIndex.uiImg));
		loadDefaultUI();
	}
	public void createDialogBox(String prompt)
	{
		mWindow.invalidate();
		mWindow.clear();
		//Pane testPane = new Pane(mStage, uiFactory.getStyle("bluepane",Pane.PaneStyle.class));
		Label testLabel = new Label("Close",uiFactory.getStyle("default",Label.LabelStyle.class), prompt);
		Button testButton = new Button("CloseButton", uiFactory.getStyle("defaultbutton",Button.ButtonStyle.class), "Close");
		//first letter = object type, second letter = what it is, this case: Dialog = D, therefore: tD = labelDialog.
//		mWindow.register("lD",testLabel);
//		mWindow.register("pD",testPane);
//		mWindow.register("bD",testButton);
		//mWindow.add(testPane).align("top,center");
		mWindow.add(testLabel);
		mWindow.add(testButton).padLeft(50);
		//interesting note: If you reference a registered object in parse, it will override what ever is added into another object. Experiment with this 
		//by adding [bD] next to [pD]. It will NOT create a copy of the button, it will override the location of it. To be outside of the pane.
//		mWindow.parse("pad:10"
//			     +"*align:top"
//			     +"---"
//				 +"[pD]");
		

		testButton.setClickListener(new ClickListener(){
			@Override
			public void click(Actor actor)
			{	
				closeDialog();
			}
		});
	}
	public void createBiDialogBox(String prompt,String yesOption, String noOption)
	{
		mWindow.invalidate();
		mWindow.clear();
		Label testLabel = new Label("Close",uiFactory.getStyle("saborDefault", Label.LabelStyle.class), prompt);	
		Button trueButton = new Button("trueButton", uiFactory.getStyle("defaultbutton",Button.ButtonStyle.class), yesOption);
		Button falseButton = new Button("falseButton", uiFactory.getStyle("defaultbutton",Button.ButtonStyle.class), noOption);		
		mWindow.add(testLabel).center().padBottom(10);
		mWindow.row();
		mWindow.add(trueButton).left();
		mWindow.add(falseButton).right();
		//mWindow.add(testPane).top().left();
		
		
		
		falseButton.setClickListener(new ClickListener(){
			@Override
			public void click(Actor actor)
			{
				//YES ACTION
				closeDialog();
			}
		});
		trueButton.setClickListener(new ClickListener(){
			@Override
			public void click(Actor actor)
			{
				//NO ACTION
				closeDialog();
			}
		});
	}
	public void closeDialog()
	{
		loadDefaultUI();
	}
	public void loadDefaultUI()
	{
		mWindow.invalidate();
		mWindow.clear();
		
		Button scanButton = new Button("Scan", uiFactory.getStyle("defaultbutton",Button.ButtonStyle.class), "ScanButton");
		Button menuButton = new Button("Menu", uiFactory.getStyle("defaultbutton",Button.ButtonStyle.class), "MenuButton");
		Button editButton = new Button("Edit", uiFactory.getStyle("defaultbutton",Button.ButtonStyle.class), "EditButton");
		Label welcomeLabel = new Label(mGameScreen.mPlayer.welcomeMessage, uiFactory.getStyle("saborLightgreen",Label.LabelStyle.class),"WelcomeLabel");

		//mWindow.defaults().spaceBottom(10);
		mWindow.debug();
		TableLayout layout  = mWindow.getTableLayout();
		//layout.add(scanButton).align("top");
		//mWindow.add(scanButton).align("top");
		
		
		//mWindow.add(menuButton).fill(0f,0f);
		//mWindow.add(editButton).fill(0f,0f);

		mWindow.register("scanBtn",scanButton);
		mWindow.register("welcome", welcomeLabel);
//		mWindow.register("menuBtn",menuButton);
//		mWindow.register("editBtn",editButton);
		//Names come from Register
		mWindow.parse("align:top,left"
				     +""
					 +"---"
					 +"[welcome] align:top,left"
					 +"---"
					 +"[scanBtn] align:center,left");
		
		menuButton.setClickListener(new ClickListener(){
			@Override
			public void click(Actor actor)
			{
				//mGameScreen.gameCamera.translate(mGameScreen.gameCamera);
				//mWindow.invalidateHierarchy();
				//createDialogBox("this would open a menu");
			}
		});
		editButton.setClickListener(new ClickListener(){
			@Override
			public void click(Actor actor)
			{
				//mGameScreen.gameCamera.translate(mGameScreen.gameCamera);
				//mWindow.invalidateHierarchy();
				//createBiDialogBox("Leave Edit Mode?", "Yes", "No");
			}
		});
		scanButton.setClickListener(new ClickListener(){
			@Override
			public void click(Actor actor)
			{
				//mGameScreen.gameCamera.translate(mGameScreen.gameCamera);
				//mWindow.invalidateHierarchy();
				//createDialogBox("You Scanned! \n These words say this \n and these words don\'t.");
			}
		});
		mStage.addActor(mWindow);
	}
	public void updateText()
	{
		//DOESN'T WORK LOL Figure that out eventually?
//		mWindow.add(debugText);
//		debugText = uiFactory.newLabel("debugText", debugString, "saborRed");
//		mWindow.register("debugText",debugText);
//		mWindow.parse("align:bottom,left"
//			     +"*align:bottom,left"
//				 +"---"
//				 +"[tDebug] align:bottom,left");
	}
	public Table getPreppedWindow()
	{
		return mWindow;
	}
}
